package com.hdoit.apollo.web.purchase.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.common.enums.purchase.PurchaseExportExcelEnum;
import com.hdoit.apollo.logical.purchase.mapper.PurchaseCollocationMapper;
import com.hdoit.apollo.logical.purchase.model.PurchaseCollocation;
import com.hdoit.apollo.logical.purchase.model.PurchaseCollocationStat;
import com.hdoit.apollo.logical.purchase.service.IPurchaseCollocationService;
import com.hdoit.apollo.logical.viewmodel.purchase.PurchaseCollocationSearch;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.setting.export.excel.ExportModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年05月23日 11:05:52
 * @comment
 */
@Controller
@RequestMapping(value = "/purchase/report/")
public class ReportController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/purchase/report/";
    }

    @Autowired
    private IPurchaseCollocationService purchaseCollocationService;
    
    @Autowired
    private PurchaseCollocationMapper purchaseCollocationMapper;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String deleteViewName = "delete";
    
    private final String exportExcelViewName="exportExcel";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,PurchaseCollocationSearch search,boolean ajaxPost){

    	PurchaseCollocationStat stat=purchaseCollocationMapper.queryStat(search);
    	
    	
        pagerModel.setTotalAmount(purchaseCollocationService.queryCountMP(search));
        List<PurchaseCollocation> purchaseCollocations= purchaseCollocationService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("purchaseCollocations", purchaseCollocations);
        model.addAttribute("purchaseCollocationSearch", search);
        model.addAttribute("stat", stat);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = exportExcelViewName, method = { RequestMethod.GET,RequestMethod.POST })
	@ResponseBody
	public String exportExcel(PurchaseCollocationSearch search){
		List<PurchaseCollocation> purchaseCollocations = purchaseCollocationService.queryListMP(search,null);
		
		ExportModel em=new ExportModel();
		em.setExcelInfoCode(PurchaseExportExcelEnum.cghz);
		em.setDataSource(purchaseCollocations);
		
		String cacheId=CommonUtils.getKeyId();
		cached.put(cacheId, em,60);
		
		return cacheId;
	}

   

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(purchaseCollocationService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}